// 1. 下载express
// npm i express
// 2. 导入express
const express = require('express');
// 3. 创建express 服务器
const app = express();

// 6. 配置解析前端传递给后端的数据
app.use(express.json({ limit: '50mb' }))
app.use(express.urlencoded({ extended: false, limit: '50mb' }))
// 5. 配置cores 跨域  npm i cors
const cors = require('cors')
// 挂载在express 上实现跨域请求
app.use(cors());
// 配置静态资源托管 图片
app.use('/static', express.static('./static'))
// http://127.0.0.1:3006/static/hotShop/apple12.jpg
// 7. 封装一个响应的方法
app.use((req, res, next) => {
    // status =1 表示接口数据错误
    res.lose = (err, status = 1) => {
        res.send({
            status,
            message: err instanceof Error ? err.message : err
        })
    }
    next();
})
// 8. 挂载业务路由 home
const home = require('./router/home')
app.use('/home', home)
const news = require('./router/news')
app.use('/news', news)
const pics = require('./router/pics')
app.use('/pics', pics)
const shopRouter = require('./router/shop')
app.use('/shop', shopRouter)
const ai = require('./router/ai')
app.use('/ai', ai)
//4.启动服务
app.listen(3006, () => {
    console.log('http://127.0.0.1:3006');
})